package com.itheima.controller;

import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 分页条件查询检查项信息
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.findByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping
    public Result add(@RequestBody CheckItem checkItem) {
        checkItemService.addCheckItem(checkItem);
        return Result.success("新增检查项成功");
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean result = checkItemService.removeById(id);
        if (result) {
            return Result.success("删除检查项成功");
        } else {
            return Result.error("删除检查项失败");
        }
    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        return Result.success(checkItem, "查询检查项成功");
    }

    /**
     * 编辑检查项
     *
     * @param checkItem
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody CheckItem checkItem) {
        checkItemService.edit(checkItem);
        return Result.success("编辑检查项成功");
    }

    /**
     * 查询所有检查项数据
     *
     * @return
     */
    @GetMapping("/list")
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemService.list();
        return Result.success(checkItemList, "查询检查项成功");
    }
}
